﻿using System.Web;
using System.Web.Services;
using Ext.Net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using System.Collections.Generic;
using WEB.Core.Util;
using System;

namespace WEB.APP.Modules.List
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Search : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";

            string type = context.Request["type"];
            var start = !string.IsNullOrEmpty(context.Request["start"]) ? int.Parse(context.Request["start"]) : 0;
            var limit = !string.IsNullOrEmpty(context.Request["limit"]) ? int.Parse(context.Request["limit"]) : 50;
            var query = !string.IsNullOrEmpty(context.Request["query"]) ? context.Request["query"] : string.Empty;

            int Total = 0;

            if (type == "StudentLoadCv")
            {
                bool haveCv = !string.IsNullOrEmpty(context.Request["haveCv"]) ? Convert.ToBoolean(context.Request["haveCv"]) : true;

                List<StudentEntity> _resul = (List<StudentEntity>)StudentService.GetInstance().LoadCv(query, haveCv, start, limit, out Total);
                context.Response.Write(string.Format("{{total:{1},'Search':{0}}}", JSON.Serialize(_resul), Total));
            }
            else if (type == "ClientLoadProject")
            {
                var ClientE = new ClientEntity()
                {
                    Keyword = query,
                    Start = start,
                    EndNum = limit
                };
                List<ClientEntity> _resul = (List<ClientEntity>)ClientService.GetInstance().Find(ClientE);
                context.Response.Write(string.Format("{{total:{1},'Search':{0}}}", JSON.Serialize(_resul), ClientE.TotalRow));
            }
            else if (type == "StudentLoadHaveProject")
            {
                bool haveProject = !string.IsNullOrEmpty(context.Request["haveProject"]) ? Convert.ToBoolean(context.Request["haveProject"]) : true;
                var StudentE = new StudentEntity()
                {
                    Keyword = query,
                    Start = start,
                    EndNum = limit,
                    HaveProject = haveProject
                };
                List<StudentEntity> _resul = (List<StudentEntity>)StudentService.GetInstance().LoadHaveProject(StudentE);
                context.Response.Write(string.Format("{{total:{1},'Search':{0}}}", JSON.Serialize(_resul), StudentE.TotalRow));
            }
            else if (type == "ProjectSearch")
            {
                var ProjectE = new ProjectEntity()
                {
                    Keyword = query,
                    Start = start,
                    EndNum = limit
                };
                List<ProjectEntity> _resul = (List<ProjectEntity>)ProjectService.GetInstance().Find(ProjectE);
                context.Response.Write(string.Format("{{total:{1},'Search':{0}}}", JSON.Serialize(_resul), ProjectE.TotalRow));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
